package com.jalarbee.core.spring.bootstrap;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;

public class CustomPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	
	private static final Logger logger = Logger.getLogger(CustomPropertyPlaceholderConfigurer.class);
	@Override
	public void setLocation(Resource location) {
		if(logger.isDebugEnabled()) {
			logger.debug("Spring : setting place holder properties.\n");
		}
		if(location != null) {
			try {
				Resource resource = location.createRelative(location.getFilename());
				super.setLocation(resource);
			} catch (IOException e) {
				logger.error("Spring : Error replacing property place holders.\n " + e.getMessage());
				e.printStackTrace();
			}
		} else {
			logger.warn("Spring : Attended to set a null location.\n");
		}
		
	}
	
	@Override
	public void setLocations(Resource[] locations) {
		if(logger.isDebugEnabled()) {
			logger.debug("Spring : setting place holder properties.\n");
		}
		if(locations != null && locations.length > 0) {
			Resource[] resources = new Resource[locations.length];
			for(int i = 0; i < locations.length; i++) {
				try {
					resources[i] = locations[i].createRelative(locations[i].getFilename());
				} catch (IOException e) {
					logger.error("Spring : Error replacing property place holders.\n " + e.getMessage());
					e.printStackTrace();
				}
			}
			super.setLocations(resources);
		} else {
			logger.warn("Spring : Attended to set a null location.\n");
		}
	}
	

}
